package wh.coach._ser;

import java.util.Arrays;
import java.util.Objects;

/**
 * @author hangwen
 * @date 2023/7/15
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("write read java bean");
        Person person = new Person(1L, "Jack", 20);

        final byte[] data = PersonSerAndDeSer.write(person);
        final Person decodedPerson = PersonSerAndDeSer.read(data);

        System.out.println("decodedPerson equals to person:" + (Objects.equals(person, decodedPerson)));

        Profiler.profile("json ser", Main::json, 1);
        Profiler.profile("binary ser", Main::binary, 1);
    }

    private static void binary() {
        Person person = new Person(1L, "Jack", 20);

        final byte[] data = PersonSerAndDeSer.write(person);
        System.out.println(Arrays.toString(data));

        if (data != null) {
            final Person decodedPerson = PersonSerAndDeSer.read(data);
            System.out.println("decodedPerson: " + Objects.toString(decodedPerson, "null"));
        } else {
            System.out.println("Json不能为空");
        }

        final Person decodedPerson = PersonSerAndDeSer.read(data);

    }

    private static void json() {
        Person person = new Person(1L, "Jack", 20);

        String json = JsonHelper.writeAsString(person);
        System.out.println(Arrays.toString(json.getBytes()));

        if (json != null) {
            Person decodedPerson = JsonHelper.readFromJson(json, Person.class);
            System.out.println("decodedPerson: " + Objects.toString(decodedPerson, "null"));
        } else {
            System.out.println("Json不能为空");

        }
    }
}
